package com.tuanzi.loan.business.repository.businesssystem.impl;

import com.tuanzi.loan.business.repository.businesssystem.custom.BusinessIncomeRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.businesssystem.BusinessIncomeRequest;
import com.tuanzi.loan.vo.response.businesssystem.BusinessIncomeResponse;
import org.jooq.Condition;
import org.jooq.impl.DSL;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanBusinessChannel.LOAN_BUSINESS_CHANNEL;
import static com.tuanzi.loan.generated.jooq.loan.tables.LoanBusinessChannelMain.LOAN_BUSINESS_CHANNEL_MAIN;
import static com.tuanzi.loan.generated.jooq.loan.tables.LoanBusinessIncome.LOAN_BUSINESS_INCOME;

public class BusinessIncomeRepositoryImpl extends JOOQBaseRepositoryPlus implements BusinessIncomeRepositoryCustom {
	@Override
	protected TableImpl getTable() {
		return LOAN_BUSINESS_INCOME;
	}

	@Override
	public Page<BusinessIncomeResponse> list(BusinessIncomeRequest searchRequest, Pageable pageable) {
		List<Integer> id = searchRequest.getChannelId();
		Integer companyId = searchRequest.getCompanyId();
		String channelName = searchRequest.getChannelName();
		String creator = searchRequest.getCreator();
		Date startTime = searchRequest.getStartTime();
		Date endTime = searchRequest.getEndTime();

		fields(LOAN_BUSINESS_INCOME.fields());
		fields(LOAN_BUSINESS_CHANNEL.CHANNEL_NAME, LOAN_BUSINESS_CHANNEL.SETTLEMENT_MODE, LOAN_BUSINESS_CHANNEL_MAIN.CHANNEL_MAIN);
		leftJoin(LOAN_BUSINESS_CHANNEL, LOAN_BUSINESS_CHANNEL.CHANNEL_ID.eq(LOAN_BUSINESS_INCOME.CHANNEL_ID.cast(Integer.class)));
		leftJoin(LOAN_BUSINESS_CHANNEL_MAIN, LOAN_BUSINESS_INCOME.CHANNEL_MAIN_ID.eq(LOAN_BUSINESS_CHANNEL_MAIN.ID));
		if (id == null || id.isEmpty()) {

		} else {
			in(LOAN_BUSINESS_CHANNEL.ID, id);
		}
		in(LOAN_BUSINESS_CHANNEL.CHANNEL_TYPE,searchRequest.getChannelType());
		eq(LOAN_BUSINESS_INCOME.CHANNEL_MAIN_ID, companyId);
		eq(LOAN_BUSINESS_INCOME.CREATOR,creator);
		contains(LOAN_BUSINESS_CHANNEL.CHANNEL_NAME, channelName);
		between(LOAN_BUSINESS_INCOME.TIME, startTime, endTime);
		orderBy(LOAN_BUSINESS_INCOME.TIME, Sort.Direction.DESC);
		Page<BusinessIncomeResponse> search = search(pageable, record -> {
			BusinessIncomeResponse response = new BusinessIncomeResponse();
			response.setId(record.get(LOAN_BUSINESS_INCOME.ID));
			response.setChannelId(record.get(LOAN_BUSINESS_INCOME.CHANNEL_ID, String.class));
			response.setChannelName(record.get(LOAN_BUSINESS_CHANNEL.CHANNEL_NAME));
			response.setBelongAppId(record.get(LOAN_BUSINESS_INCOME.BELONG_APP_ID));
			response.setChannelType(record.get(LOAN_BUSINESS_INCOME.CHANNEL_TYPE));
			response.setChannelMain(record.get(LOAN_BUSINESS_CHANNEL_MAIN.CHANNEL_MAIN));
			response.setSettlementTime(record.get(LOAN_BUSINESS_INCOME.TIME));
			response.setPayMode(record.get(LOAN_BUSINESS_INCOME.PAY_MODE));
			response.setPayType(record.get(LOAN_BUSINESS_INCOME.PAY_TYPE));
			response.setCopMode(record.get(LOAN_BUSINESS_CHANNEL.SETTLEMENT_MODE));
			response.setPricePay(record.get(LOAN_BUSINESS_INCOME.PRICE_PAY));
			response.setCount(record.get(LOAN_BUSINESS_INCOME.COUNT));
			response.setIncomeAmount(record.get(LOAN_BUSINESS_INCOME.INCOME_AMOUNT));
			response.setRealRegister(record.get(LOAN_BUSINESS_INCOME.REAL_COST) == null ? 0 : record.get(LOAN_BUSINESS_INCOME.REAL_COST).intValue());
			response.setBeforeRealCost(record.get(LOAN_BUSINESS_INCOME.BEFORE_REAL_COST) == null ? 0 : record.get(LOAN_BUSINESS_INCOME.BEFORE_REAL_COST));
			response.setRemark(record.get(LOAN_BUSINESS_INCOME.REMARK));
			response.setCreator(record.get(LOAN_BUSINESS_INCOME.CREATOR));
			response.setCreateTime(record.get(LOAN_BUSINESS_INCOME.CREATE_TIME));
			response.setUpdator(record.get(LOAN_BUSINESS_INCOME.UPDATOR));
			response.setUpdateTime(record.get(LOAN_BUSINESS_INCOME.UPDATE_TIME));
			response.setVersion(record.get(LOAN_BUSINESS_INCOME.VERSION));
			response.setLoginCount(record.get(LOAN_BUSINESS_INCOME.LOGIN_COUNT));
			return response;
		});

		return search;
	}

	@Override
	public Double getAllIncome(BusinessIncomeRequest $) {
		List<Integer> channelId = $.getChannelId();
		Integer companyId = $.getCompanyId();
		Date startTime = $.getStartTime();
		Date endTime = $.getEndTime();
		Condition condition = LOAN_BUSINESS_INCOME.ID.isNotNull();
		if (channelId != null && !channelId.isEmpty())
			condition = LOAN_BUSINESS_CHANNEL.ID.in(channelId == null ? new ArrayList<Integer>() : channelId);
		if (companyId != null)
			condition = condition.and(LOAN_BUSINESS_INCOME.CHANNEL_MAIN_ID.eq(companyId));
		if (startTime != null && endTime != null)
			condition = condition.and(LOAN_BUSINESS_INCOME.TIME.ge(new java.sql.Date(startTime.getTime()))).and(LOAN_BUSINESS_INCOME.TIME.le(new java.sql.Date(endTime.getTime())));
		Double into = dslContext.select(DSL.sum(LOAN_BUSINESS_INCOME.INCOME_AMOUNT)).from(LOAN_BUSINESS_INCOME).leftJoin(LOAN_BUSINESS_CHANNEL)
				.on(LOAN_BUSINESS_CHANNEL.CHANNEL_ID.eq(LOAN_BUSINESS_INCOME.CHANNEL_ID.cast(Integer.class))
						.and(LOAN_BUSINESS_CHANNEL.COMPANY_ID.eq(LOAN_BUSINESS_INCOME.CHANNEL_MAIN_ID))).where(condition).fetchOne().into(Double.class);

		return into;
	}
}
